package org.example.MedicalCrowdsourcing.mapper;

import org.example.MedicalCrowdsourcing.model.dto.ProjectCreateDTO;
import org.example.MedicalCrowdsourcing.model.dto.ProjectDTO;
import org.example.MedicalCrowdsourcing.model.entity.Project;
import org.springframework.stereotype.Component;

import java.util.stream.Collectors;

/**
 * 项目实体与DTO之间的转换器
 */
@Component
public class ProjectMapper {

    /**
     * 将Project实体转换为ProjectDTO
     */
    public ProjectDTO toDto(Project project) {
        if (project == null) {
            return null;
        }

        ProjectDTO dto = new ProjectDTO();
        dto.setId(project.getId());
        dto.setTitle(project.getTitle());
        dto.setDescription(project.getDescription());
        dto.setStartDate(project.getStartDate());
        dto.setEndDate(project.getEndDate());
        dto.setRequiredParticipants(project.getRequiredParticipants());
        dto.setRequiredDataTypes(project.getRequiredDataTypes());
        dto.setParticipantCriteria(project.getParticipantCriteria());
        dto.setCompensationAmount(project.getCompensationAmount());
        dto.setStatus(project.getStatus());
        dto.setCreatedAt(project.getCreatedAt());
        dto.setUpdatedAt(project.getUpdatedAt());
        
        if (project.getResearcher() != null) {
            dto.setResearcherId(project.getResearcher().getId());
            dto.setResearcherName(project.getResearcher().getUsername());
        }
        
        if (project.getParticipants() != null) {
            dto.setParticipantsCount(project.getParticipants().size());
            dto.setParticipants(project.getParticipants().stream()
                    .map(user -> user.getId().toString())
                    .collect(Collectors.toList()));
        }
        
        return dto;
    }

    /**
     * 将ProjectCreateDTO转换为Project实体
     */
    public Project toEntity(ProjectCreateDTO dto) {
        if (dto == null) {
            return null;
        }

        Project project = new Project();
        project.setTitle(dto.getTitle());
        project.setDescription(dto.getDescription());
        project.setStartDate(dto.getStartDate());
        project.setEndDate(dto.getEndDate());
        project.setRequiredParticipants(dto.getRequiredParticipants());
        project.setRequiredDataTypes(dto.getRequiredDataTypes());
        project.setParticipantCriteria(dto.getParticipantCriteria());
        project.setCompensationAmount(dto.getCompensationAmount());
        
        return project;
    }
} 